package com.jumper.mydemo.statics;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.metrics.ApplicationStartup;

import com.jumper.mydemo.my.MyConfiguration;

public class ConfigurableBeanFactoryDemo {
    @SuppressWarnings("resource")
    public static void run() {
        ConfigurableBeanFactory configurableBeanFactory = new AnnotationConfigApplicationContext(MyConfiguration.class).getBeanFactory();

        configurableBeanFactory.setParentBeanFactory(new DefaultListableBeanFactory());

        int beanPostProcessorCount = configurableBeanFactory.getBeanPostProcessorCount();
        System.out.println("获取BeanPostProcessor数量:" + beanPostProcessorCount);

        String[] scopeNames = configurableBeanFactory.getRegisteredScopeNames();
        System.out.println("获取所有已注册的Scope名称:" + String.join(",", scopeNames));

        Scope customScope = configurableBeanFactory.getRegisteredScope("customScope");
        System.out.println("获取注册的Scope:" + customScope);

        ApplicationStartup applicationStartup = configurableBeanFactory.getApplicationStartup();
        System.out.println("获取ApplicationStartup:" + applicationStartup);

        ConfigurableListableBeanFactory otherFactory = new DefaultListableBeanFactory();
        configurableBeanFactory.copyConfigurationFrom(otherFactory);
        System.out.println("拷贝配置copyConfigurationFrom:" + otherFactory);

        String beanName = "myService";
        String alias = "helloService";
        configurableBeanFactory.registerAlias(beanName, alias);
        System.out.println("注册别买registerAlias, BeanNames:" + beanName + ", alias:" + alias);

        BeanDefinition mergedBeanDefinition = configurableBeanFactory.getMergedBeanDefinition("myService");
        System.out.println("获取合并后的BeanDefinition:" + mergedBeanDefinition);

        String factoryBeanName = "myService";
        boolean isFactoryBean = configurableBeanFactory.isFactoryBean(factoryBeanName);
        System.out.println("判断是否为FactoryBean:" + isFactoryBean);

        String currentBeanName = "myService";
        boolean isCreation = true;
        configurableBeanFactory.setCurrentlyInCreation(currentBeanName, isCreation);
        System.out.println("设置当前Bean是否正在创建:" + currentBeanName);

        boolean isCurrentlyInCreation = configurableBeanFactory.isCurrentlyInCreation(currentBeanName);
        System.out.println("判断当前Bean是否正在创建:" + isCurrentlyInCreation);

        String dependentBeanName = "dependentBean";
        configurableBeanFactory.registerDependentBean(beanName, dependentBeanName);
        System.out.println("注册依赖关系, beanName:" + beanName + ", dependentName:" + dependentBeanName);

        String[] dependentBeans = configurableBeanFactory.getDependentBeans(beanName);
        System.out.println("获取所有依赖与指定Bean的Bean名称:" + String.join(",", dependentBeans));

        String[] dependencies = configurableBeanFactory.getDependenciesForBean(beanName);
        System.out.println("获取指定Bean依赖的所有Bean名称:" + String.join(",", dependencies));

        Object beanInstance = configurableBeanFactory.getBean(beanName);
        configurableBeanFactory.destroyBean(beanName, beanInstance);
        System.out.println("销毁指定Bean实例:" + beanName);

        configurableBeanFactory.destroySingletons();
        System.out.println("销毁所有单例Bean");
    }
}
